import instance from "../../axios/index";
import {
  Button,
  Form,
  Input,
  message,
} from "antd";
function Addarticle() {
  // console.log(123);
  // 表单
  const [form] = Form.useForm();
  //点击新增文章
  const address = () => {
    // 获取表单数据
    const fromData = form.getFieldsValue();
    console.log(fromData.title, fromData.textArea);
    if (fromData.title != undefined && fromData.textArea != undefined) {
      if (fromData.title.length < 5) {
        message.error("标题不能少于5个字");
      } else {
        const data = {
          homeallid: localStorage.getItem("homeallid"),
          title1: fromData.title,//标题
          content1: fromData.textArea,//内容
        };
        // 调用添加接口
        instance.post("/api/doctoraddarticle", data).then((res) => {
          console.log(res);
          if (res.data.code == 200) {
            message.success("新增成功");
          } else {
            message.error("新增失败");
          }
        });
      }

      form.resetFields();
    } else {
      message.error("内容或标题不能为空");
    }
  }
  return (
    <div>
      <Form
        form={form}
        layout="vertical"
        name="basic"
        labelCol={{ span: 5 }}
        wrapperCol={{ span: 15 }}
        initialValues={{ remember: true }}
        autoComplete="off"
      >
        <Form.Item
          layout="vertical"
          name="title"
          labelCol={{ span: 30, offset: 20 }}
          wrapperCol={{ span: 30 }}
          rules={[{ required: true, message: '请输入您的文章标题' }]}
        >
          <Input.TextArea rows={3} placeholder="请输入文章标题(5~100个字)" />
        </Form.Item>
        <Form.Item
          name="textArea"
          labelCol={{ span: 30 }}
          wrapperCol={{ span: 30 }}
          rules={[{ required: true, message: '请输入您的文章内容' }]}
        >
          <Input.TextArea rows={12} placeholder="请输入文章内容" />
        </Form.Item>
        <Button style={{ float: 'right' }} type="primary" shape="round" onClick={address}>确认新增</Button>
      </Form>
    </div>
  );
}

export default Addarticle;
